//app.js
App({
  onLaunch: function() {
    this.getLocation()
    wx.login({
      success: function(res) {
        // console.log(res)
        wx.getUserInfo({
          success: function(res) {
            // console.log(res)
            if(res.errMsg=='getUserInfo:ok'){
                var flag=0
                var info=res.userInfo
                var name=info.nickName
                var headImg=info.avatarUrl
                var uuid=res.iv
            }else{
                var flag=1
            }
            wx.request({
                url:'https://www.vaniswdq.com/saveMyInfo',
                method:'post',
                data: {
                    flag:flag,
                    name:name,
                    headImg:headImg,
                    uuid:uuid
                },
                header: {
                'content-type':'application/x-www-form-urlencoded'
                },
                success: function(res) {
                    // console.log(res.data)
                    var getUserInfo=res.data.dataWrapper.tbUser
                    wx.setStorageSync('userInfo',getUserInfo)
                }
            })
            
          }
        })
      }
    })
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              // console.log(res);
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData: {
    userInfo: null,
  },
  getLocation:function(){
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        // console.log(res)
        var latitude=res.latitude
        var longitude=res.longitude
        wx.setStorageSync('long', longitude)
        wx.setStorageSync('lat', latitude)
          wx.request({
            url:'https://www.vaniswdq.com/getLocation',
            method:'post',
            data: {
                lat:longitude,
                log:latitude,
            },
            header: {
              'content-type':'application/x-www-form-urlencoded'
            },
            success: function(res) {
                // console.log(res.data)
                if(res.data.status=='-1'){
                    wx.showModal({
                      title: '提示',
                      content: res.data.msg,
                      showCancel:false
                      })
                }else{
                  wx.setStorageSync('location', res.data.dataWrapper.address)
                }
            }
        })
      }
    })
  },
  showToast(msg, icon, time) {
    wx.hideToast()
    wx.showToast({
      title: msg || '正在请求',
      icon: icon || 'loading',
      duration: time || 3000
    })
  }
})